package com.D5Power.Editor
{
	import com.D5Power.Objects.GameObject;
	import com.D5Power.Objects.NCharacterObject;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import org.aswing.BorderLayout;
	import org.aswing.BoxLayout;
	import org.aswing.FlowLayout;
	import org.aswing.Insets;
	import org.aswing.JButton;
	import org.aswing.JFrame;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.JTextArea;
	import org.aswing.SoftBoxLayout;
	import org.aswing.border.EmptyBorder;
	import org.aswing.event.FrameEvent;
	import org.aswing.ext.Form;
	
	/**
	 * Lua脚本编辑器
	 */ 
	public class LuaEditor extends JFrame
	{
		private var _box:JPanel;
		private var _text:JTextArea;
		private var _url:String;
		private var _target:*;
		private var _callfrom:*;

		public function LuaEditor(owner:*=null,url:String='',target:*=null,callfrom:*=null)
		{
			super(owner, 'Lua脚本编辑器', false);
			_target = target;
			_callfrom = callfrom;
			if(url=='')
			{
				init();
			}else{
				loadLuaScript(url);
			}
		}
		
		
		
		private function loadLuaScript(filename:String):void
		{
			var f:File;
			
			switch(_target.objectName)
			{
				case 'NCharacterObject':
					f = File.applicationDirectory.resolvePath(Global.LUAPATH+'npc/'+filename)
					break;
				case 'MapShower':
					f = File.applicationDirectory.resolvePath(Global.LUAPATH+'scene/'+filename)
					break;
				default:
					return;
					break;
			};
			var fs:FileStream = new FileStream();
			fs.open(f,FileMode.READ);
			init(fs.readUTFBytes(fs.bytesAvailable));
		}
		
		private function init(script:String=''):void
		{
			_text = new JTextArea(script,24,22);

			
			var bt_ok:JButton = new JButton('保存');
			var bt_cancle:JButton = new JButton('取消');
			var bt_run:JButton = new JButton('运行');
			
			bt_cancle.addActionListener(onCancle);
			bt_ok.addActionListener(onSubmit);
			bt_run.addActionListener(onRun);
			
			var jpanle:JPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,16,5));
			jpanle.appendAll(bt_ok,bt_cancle,bt_run);
			
			
			getContentPane().append(new JScrollPane(_text),BorderLayout.CENTER);
			getContentPane().append(buildHelper(),BorderLayout.EAST);
			getContentPane().append(jpanle,BorderLayout.SOUTH);
			setSizeWH(300,400);
			setLocationXY(400,150);
		}
		
		private function buildHelper():JPanel
		{
			var helper:JPanel = new JPanel(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			
			var bt_me:JButton = new JButton('访问自己');
			bt_me.addActionListener(function():void{_text.setText(_text.getText()+'me')});
			helper.append(bt_me);
			
			switch(_target.objectName)
			{
				case 'NCharacterObject':
					var bt_ctrl:JButton = new JButton('访问控制器');
					bt_ctrl.addActionListener(function():void{_text.setText(_text.getText()+'me.controler')});
					var bt_point:JButton = new JButton('新建点');
					bt_point.addActionListener(function():void{_text.setText(_text.getText()+'as3.class.flash.geom.Point.new()')});
					var bt_obj:JButton = new JButton('新建其他对象');
					bt_obj.addActionListener(function():void{_text.setText(_text.getText()+'as3.class.')});
					bt_obj.setBorder(new EmptyBorder(null,new Insets(0,0,10,0)));
					
					var bt_function_moveto:JButton = new JButton('移动到');
					bt_function_moveto.addActionListener(function():void{_text.setText(_text.getText()+'me.controler.moveTo();')});
					var bt_function_movepath:JButton = new JButton('巡逻');
					bt_function_movepath.addActionListener(function():void{_text.setText(_text.getText()+'me.controler.moveInPath();\nme.controler.loop=true;')});

					helper.append(bt_ctrl);
					helper.append(bt_point);
					helper.append(bt_obj);
					helper.append(bt_function_moveto);
					helper.append(bt_function_movepath);
					break;
				
				case 'MapShower':
					
					var bt_global:JButton = new JButton('访问全局变量');
					bt_global.addActionListener(function():void{_text.setText(_text.getText()+'Global')});
					
					helper.append(bt_global);
					break;
			}
			
			return helper;
		}
		
		private function onCancle(e:Event):void
		{
			closeReleased();
		}
		
		private function onSubmit(e:Event):void
		{
			var file:File;
			
			switch(_target.objectName)
			{
				case 'NCharacterObject':
					file = File.applicationDirectory.resolvePath(Global.LUAPATH+'npc/');
					break;
				
				case 'MapShower':
					file = File.applicationDirectory.resolvePath(Global.LUAPATH+'scene/');
					break;
				default:
					return;
					break;
			}
			
			file.browseForSave('脚本另存为：');
			file.addEventListener(Event.SELECT,saveScript);
			
			closeReleased();
		}
		
		private function saveScript(e:Event):void
		{
			var file:File = e.target as File;
			var stream:FileStream=new FileStream();
			stream.open(file,FileMode.WRITE);
			stream.writeUTFBytes(_text.getText());
			stream.close();
			
			try
			{
				// 尝试呼叫回调函数
				_callfrom.luaEditorBack(file.name);
			}catch(e:Error){
				
			}
		}
		
		private function onRun(e:Event):void
		{
			switch(_target.objectName)
			{
				case 'NCharacterObject':
					var nobj:NCharacterObject = _target as NCharacterObject;
					try
					{
						nobj.runLua(_text.getText());
					}catch(e:Error){
						trace('脚本错误');
					}
					break;
			}
		}
	}
}